package com.demo.zhihu.web;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.demo.zhihu.model.Answer;
import com.demo.zhihu.model.Comment;
import com.demo.zhihu.model.Question;
import com.demo.zhihu.service.QuestionService;

import cn.techarts.dragonfly.web.Controller;
import cn.techarts.dragonfly.web.WebContext;
import cn.techarts.dragonfly.web.rest.Post;

@Singleton
@Controller
public class QuestionWebService {
	
	@Inject
	private QuestionService service;
	
	@Post("/question/{asker}")
	public Question ask(WebContext arg) {
		var asker = arg.intAt(0);
		var title = arg.get("title");
		var summary = arg.get("summary");
		return service.ask(asker,title,summary);
	}
	
	@Post("/question/{id}/answer/{user}")
	public Answer answer(WebContext arg) {
		var question = arg.intAt(0);
		var user = arg.intAt(1);
		var content = arg.get("content");
		var result = service.answer(question, user, content);
		if(result == null) {
			arg.error(-11, "You can not answer the question");
		}
		return result;
	}	
	
	@Post("/answer/{id}/comment/{commenter}")
	public Comment comment(WebContext arg) {
		var answer = arg.intAt(0);
		var commenter = arg.intAt(1);
		var content = arg.get("content");
		return service.comment(answer,commenter,content);
	}
	
	@Post("/comment/{id}/reply/{replier}")
	public Comment reply(WebContext arg) {
		var comment = arg.intAt(0);
		var commenter = arg.intAt(1);
		var content = arg.get("content");
		return service.reply(comment,commenter,content);
		}
	
	@Post("/user/{id}/like/answer/{id}")
	public boolean likeAnswer(WebContext arg) {
		var user = arg.intAt(0);
		var answer =arg.intAt(1);
		return service.likeSomeone(Answer.ME,answer,user);
	}
	
	@Post("/like/comment/{id}/user/{id}")
	public boolean likeComment(WebContext arg) {
		var comment = arg.intAt(0);
		var user = arg.intAt(1);
		return service.likeSomeone(Comment.ME,comment,user);
	}
	
	
}
